package com.pdh.front.controller;

import com.pdh.bean.Message;
import com.pdh.bean.User;
import com.pdh.mvc.ResponseBody;
import com.pdh.mvc.ResponseView;
import com.pdh.service.ExpressService;
import com.pdh.util.JSONUtil;
import com.pdh.util.LoginUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Author: 彭德华
 * @Date 2021-08-29 22:19
 */
public class QRCodeController {
    @ResponseView("/front/createQRCode.do")//view:直接跳转地址
    public String createQRCode(HttpServletRequest req, HttpServletResponse resp){
        //1.接收参数
        String code = req.getParameter("code");
        String type = req.getParameter("type");//express(expressList.html) || user(index.html)
        String userPhone = null;
        String QRCodeContent = null;
        if("express".equals(type)){
            //展示用户的二维码:被扫描后，展示单个快递的信息
            //存储code
            QRCodeContent = "express_"+code;
        }else{
            //展示快递的二维码:被扫描后，快递员端(柜子)展示用户所有快递
            //存储userPhone
            User frontUser = LoginUtil.getFrontUser(req.getSession());
            userPhone = frontUser.getUserPhone();
            QRCodeContent = "userPhone_"+userPhone;
        }
        HttpSession session = req.getSession();
        //使用session存储QRCodeContent信息 在/wx/QRCode.do里面获取到
        session.setAttribute("QRCode",QRCodeContent);
        return "/personQRCode.html";
    }

    @ResponseBody("/wx/QRCode.do")
    public String getQRCode(HttpServletRequest req, HttpServletResponse resp){
        HttpSession session = req.getSession();
        String qrCode = (String) session.getAttribute("QRCode");
        Message message = new Message();
        if(qrCode == null){
            message.setStatus(-1);
            message.setResult("取件码获取出错，请用户重新操作");
        }else {
            message.setStatus(0);
            message.setResult(qrCode);
        }
        String json = JSONUtil.toJSON(message);
        //把二维码要展示的内容return出去
        return json;

    }

    @ResponseBody("/wx/updateStatus.do")
    public String updateExpressStatus(HttpServletRequest req,HttpServletResponse resp){
        String code = req.getParameter("code");
        //取件操作
        Message msg = ExpressService.updateStatus(code,2);
        Message message = new Message();
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/wx/findExpressByCode.do")
    public String findExpressByCode(HttpServletRequest req,HttpServletResponse resp){
        //1.获取取件码
        String code = req.getParameter("code");
        Message msg = ExpressService.findByCode(code);
        return JSONUtil.toJSON(msg);
    }
}
